/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainWindow.java
 *
 * Created on Jun 23, 2011, 7:25:28 PM
 */
package Game;

import GameCore.ResourceSetting;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.Timer;
import java.awt.event.*;

/**
 *
 * @author NGUYEN VAN TAM
 */
public class MainWindow extends javax.swing.JPanel implements ActionListener{

    /** Creates new form MainWindow */
    public MainWindow() {
        initComponents();
    }
    
    public Timer gameTime;
    public Game game;
    Thread t ;
    Image image = null;
    
    public void InitGame()
    {
        game = new Game();
        game.Init();
        
        game.LoadContent();
      

        
        Timer timer = new Timer(10,this); 
        timer.start();
    }

    public void UpdateMouseInfo(MouseEvent evt) {
        // TODO add your handling code here:
        //if(HandlingInput.MouseHanller.nDelay == 0)
        //{
            //HandlingInput.MouseHanller.nDelay = 1;
            if(game.gameState.playing == true) 
                HandlingInput.MouseHanller.Update(evt);
            else 
                HandlingInput.MouseHanller.Update(null);
        /*}
        else 
        {
            HandlingInput.MouseHanller.nDelay--;
        }*/
    }

    @Override
    public void actionPerformed(ActionEvent e) {
              update(null);
    }
    
    // kĩ thuật dùng  DC ảo trong java     
    @Override
    public void paint(Graphics g)
    {  
        if(image == null)
        {
            image = createImage(this.getSize().width, this.getSize().height);
            Graphics gTemp = image.getGraphics();
            game.gameState.background.Draw(gameTime, (Graphics2D)gTemp);
            gTemp.dispose();
        }
        Image image1 = createImage(this.getSize().width, this.getSize().height);

        Graphics gTemp1 = image1.getGraphics();
        gTemp1.drawImage(image, 0, 0, this);
        
        game.Draw(gameTime, (Graphics2D)gTemp1);
        g.drawImage(image1, 0, 0, this);
        gTemp1.dispose();
    }
    
    
    @Override 
    public void update(Graphics g)
    {
        game.Update(gameTime);
        repaint();
        //invalidate();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                formMouseMoved(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        UpdateMouseInfo(evt);
    }//GEN-LAST:event_formMouseClicked

    private void formMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseMoved
        // TODO add your handling code here:
        UpdateMouseInfo(evt);
    }//GEN-LAST:event_formMouseMoved

    private void formMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseReleased
        // TODO add your handling code here:
        UpdateMouseInfo(evt);
    }//GEN-LAST:event_formMouseReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
